package net.hn.hnms.sys.file.controller.feign;


import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.exception.ServiceException;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import net.hn.hnms.sys.common.core.web.controller.BaseController;
import net.hn.hnms.sys.common.oss.core.OssClient;
import net.hn.hnms.sys.common.oss.entity.UploadResult;
import net.hn.hnms.sys.common.oss.factory.OssFactory;
import net.hn.hnms.sys.file.api.domain.SysFile;
import net.hn.hnms.sys.file.api.domain.vo.SysOssVo;
import net.hn.hnms.sys.file.domain.SysOss;
import net.hn.hnms.sys.file.mapper.SysOssMapper;
import net.hn.hnms.sys.file.service.ISysOssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotEmpty;
import java.util.Arrays;
import java.util.List;

@ApiIgnore
@Slf4j
@RestController
@RequestMapping("/file")
public class FileController extends BaseController {


    @Autowired
    private SysOssMapper sysOssMapper;

    @Autowired
    private  ISysOssService iSysOssService;

    @PostMapping("/OSS_Upload")
    public SysFile upload(String name, String originalFilename, String contentType, byte[] file) throws ServiceException {
        try {
//           originalFilename 源文件名  截取文件名字  返回后缀名
            String suffix = StringUtils.substring(originalFilename, originalFilename.lastIndexOf("."), originalFilename.length());
//            创建文件服务客户端
            OssClient storage = OssFactory.instance();
//          文件上传
            UploadResult uploadResult = storage.uploadSuffix(file, suffix, contentType);
            // 保存文件信息
            SysOss oss = new SysOss();
            oss.setUrl(uploadResult.getUrl());
            oss.setFileSuffix(suffix);
            oss.setFileName(uploadResult.getFilename());
            oss.setOriginalName(originalFilename);
            oss.setService(storage.getConfigKey());
            sysOssMapper.insert(oss);
            SysFile sysFile = new SysFile();
            sysFile.setName(uploadResult.getFilename());
            sysFile.setUrl(uploadResult.getUrl());
            return sysFile;
        } catch (Exception e) {
            log.error("上传文件失败", e);
            throw new ServiceException("上传文件失败");
        }
    }
    /**
     * 删除OSS对象存储
     */
    @DeleteMapping("/{ossIds}")
    public R<Void> remove(@ApiParam("OSS对象ID串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable(value = "ossIds") Long[] ossIds) {
        return toAjax(iSysOssService.deleteWithValidByIds(Arrays.asList(ossIds), true) ? 1 : 0);
    }
    /**
     * 查询OSS对象基于id串
     */
    @GetMapping("/listByIds/{ossIds}")
    public R<List<SysOssVo>> listByIds(@ApiParam("OSS对象ID串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable(value = "ossIds") Long[] ossIds) {
        List<SysOssVo> list = iSysOssService.listByIds(Arrays.asList(ossIds));
        return R.ok(list);
    }

}
